package com.szholly.data.coderule.rules;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.szholly.utils.util.StringUtils;
import com.szholly.data.general.*;

/// <summary>
/// 定义最小未使用值编码规则
/// </summary>
@Component("CodeRule_3")
@Scope("prototype")
class MinUnusedValueRule extends CodeRuleBase {
	// / <summary>
	// / 获取代码规则值
	// / </summary>
	// / <param name="table">具体表</param>
	// / <param name="entity">要编码的实体</param>
	// / <param name="para">规则生成参数</param>
	// / <returns></returns>
	@Override
	public String GetCodeRule(ITable table, RowBase entity, String para,
			RowBase refEntity, String curCodeRule, String ruleInfoID) {
		if (table == null || entity == null || StringUtils.IsNullOrSpace(para))
			return "";

		String[] msgs = para.split("\\|");
		if (msgs.length < 2)
			return "";

		String groupFields = msgs[0];
		String maxField = msgs[1];
		if (StringUtils.IsNullOrSpace(maxField))
			return "";

		long defaultValue = 1;
		if (msgs.length > 2) {
			// long.TryParse(msgs[2], out defaultValue);
			defaultValue = Long.parseLong(msgs[2]);
		}

		getValueType = GetValueType.UnusedMin;
		String searchString = GetSearchString(table, entity, groupFields);
		return UnuseSort(table, entity, maxField, searchString, defaultValue,
				ruleInfoID);
	}
}
